package gui;

import hcs.HouseController;
import hcs.model.Room;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import activity.SetRoomParametersActivity;

/**
 * 
 * @author vincenzo.delia@polito.it
 *
 */
@SuppressWarnings("serial")
public class SetRoomParametersDialog extends JDialog {
	
	private String[] labels = {"StandardPresenceTemp", 
			"StandardAwayTemp",
			"StandardHeatingTime",
			"StandardAwayTime",
			"StandardInsideTime"};
	private Map<String,JSpinner> map;
	private HcsGuiMenu toolsMenu;
	
	public ActionListener onCancel = new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			setVisible(false);
		}		
	};
	
	
	public ActionListener onSet = new ActionListener() {

		public void actionPerformed(ActionEvent ev) {
			Collection<RoomMonitor> rms = toolsMenu.getCommandMenuBar().getMainFrame().getRoomMonitors();
			for(RoomMonitor r : rms) {
				if(r.isSelected()) {
					setRoomParameter(r.getRoom());
				}
			}
			setVisible(false);
		}
		
	}; 
	
	private void setRoomParameter(Room room) {
		int i = 0;
		for(String name : labels) {
			JSpinner s = map.get(name);
			SpinnerNumberModel m =  (SpinnerNumberModel) s.getModel();
			if(i < 2) {
				double t = m.getNumber().doubleValue();
				SetRoomParametersActivity.setRoomParameter(room, name, Double.TYPE, t);
			} else {
				int t = m.getNumber().intValue();
				SetRoomParametersActivity.setRoomParameter(room, name, Integer.TYPE, t);
			}

			i += 1;
		}
		room.getSettings().notifyObservers();
	}
	
	public SetRoomParametersDialog(HcsGuiMenu toolsMenu) {
		
		super(toolsMenu.getCommandMenuBar().getMainFrame(), "Rooms Settings");
		this.toolsMenu = toolsMenu;
		map = new HashMap<String, JSpinner>();
		Container c = this.getContentPane();
		c.setLayout(new GridLayout(6, 2));
		int i = 0;
		HouseController hc = toolsMenu.getCommandMenuBar().getMainFrame().getHouseController();
		for (String name : labels) {
			JLabel lab = new JLabel(name);
			c.add(lab);
			JSpinner spinner = null;
			if(i < 2) {
				SpinnerModel tempModel = new SpinnerNumberModel(SetRoomParametersActivity.getDefaultParameter(hc, name),0.0,100.0,0.1);
				spinner = new JSpinner(tempModel);
			} else {
				SpinnerModel tempModel = new SpinnerNumberModel(SetRoomParametersActivity.getDefaultParameter(hc,name),0,1000,1);
				spinner = new JSpinner(tempModel);
			}
			map.put(name, spinner);
			c.add(spinner);

			i += 1;
		}
		buildButton("Set",onSet,c);
		buildButton("Cancel",onCancel,c);
		this.pack();
	}
	
	private void buildButton(String label, ActionListener action, Container c) {
		JButton b = new JButton(label);
		b.addActionListener(action);
		b.setAlignmentX(Component.RIGHT_ALIGNMENT);
		c.add(b);
	}
}
